// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import App from './App';
import router from './router';
import iView from 'iview';
import 'iview/dist/styles/iview.css';
import axios from 'axios';
import store from './store';
import Cookies from 'js-cookie';
import {Modal, Notice} from 'iview';
import util from '@/lib/util.js';
import uploader from 'vue-simple-uploader'


Vue.use(iView);
Vue.use(uploader);
Vue.prototype.$http = axios;
Vue.prototype.$Modal = Modal;
Vue.prototype.$Notice = Notice;

Vue.config.productionTip = false;


/**权限指令**/
Vue.directive('hasPermissionButton', {
  bind: function (el, binding) {
    if (!Vue.prototype.$_has(binding.value)) {
      el.parentNode.removeChild(el);
    }
  }
});

Vue.prototype.$_has = function (value) {
  let permissionList = JSON.parse(localStorage.getItem('user-permissionList'));

  for(let i = 0 ; i < permissionList.length ; i++){
    if(permissionList[i].path === value){
      return true;
    }
  }
  return false;
};

//获取当前页面路由信息
router.afterEach(function (transition) {
  console.log("当前路由参数")
  console.log(transition)
});

router.beforeEach((to, from, next) => {

  if (to.path === '/login') {
    Cookies.remove('angel-bo-token');
    Cookies.remove('user-info');
    localStorage.clear();
    next();
    return;
  }
  if (to.path === '/403' || to.path === '/404' ||to.path === '/500') {
    next();
    return;
}

  if (to.path === '/index') {
    next();
    return;
  }

  var fdStart = to.path.indexOf("file");
  if(fdStart >= 0){
    next();
    return;
  }

  console.log(to.path);

  const token = Cookies.get('angel-bo-token');
  if (!token && to.path !== '/login') {
    next({
      path: '/login'
    })
  } else {

    if (token && !Cookies.get('user-info')) {
      util.post('/auth/jwt/user', token, function (data) {
        Cookies.set('user-info', data);
        //获取权限
        util.post('/admin/permission/getPermissionListMenu', {
          userId: data.userId
        }, function (data) {

          let permissionListTree = JSON.parse(JSON.stringify(data));
          util.permissionTreeToList(permissionListTree);
          localStorage.setItem('user-permissionList-Tree', JSON.stringify(permissionListTree));
          let permissionList = [];
          util.treeToList(data, permissionList);
          localStorage.setItem('user-permissionList', JSON.stringify(permissionList));
          next();
        });
      });
    } else {
      if(Vue.prototype.$_has(to.fullPath)){
        next();
      }else{
        next({
          path: '/403'
        })
      }

    }

  }

});

axios.interceptors.request.use(
  config => {
    if (Cookies.get('angel-bo-token')) {  // 判断是否存在token，如果存在的话，则每个http header都加上token
      config.headers.Authorization = Cookies.get('angel-bo-token');
    }
    return config;
  },
  err => {
    return Promise.reject(err);
  });

// 添加响应拦截器
axios.interceptors.response.use(response => {
  //console.log("拦截器相应了");
  //console.log(response.data);
  const {status, msg} = response.data;
  // TOKEN失效
  if (status === 'ANGEL-00110' || status === 'ANGEL-00101' || status === 'ANGEL-00111') {
    Modal.error({
      title: status,
      content: msg,
      okText: "返回登录",
      onOk: function () {
        router.push('/login'); // 路由跳转登录页
        Cookies.remove('angel-bo-token');
      }
    });


  } else if (status !== '200') {
    Modal.info({
      title: status,
      content: msg
    });

  }
  return response.data;

}, error => {
  return Promise.reject(error);
});

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '<App/>',
  store: store,
  render: h => h(App),
  components: {App}
});
